import { CoolController, BaseController, QueryOp } from '@cool-midway/core';
import { Body, Inject, Post, Get, Query } from '@midwayjs/core';
import { PushTemplateEntity } from '../../entity/template'; 
import { PushTemplateService } from '../../service/template'; 

/**
 * pushTemplate模块-推送模板信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: PushTemplateEntity,
  service: PushTemplateService,
  pageQueryOp: {
    keyWordLikeFields: ['name', 'title'],
    fieldEq: ['userId'],
    where: async (ctx) => {
      return [
        ['a.userId = :userId', { userId: ctx.admin?.userId }],
      ];
    },
  }
})
export class AdminPushTemplateController extends BaseController {

    @Inject()
      ctx;

    @Inject()
    pushTemplateService: PushTemplateService;

    @Post('/findByUserId', { summary: '根据用户ID查找推送模板' })
    async findByUserId(@Body('userId') userId: number) {
        return this.ok(await this.pushTemplateService.findByUserId(userId));
    }

    @Post('/findOneByUserIdAndName', { summary: '根据用户ID和模板名称查找推送模板' })
    async findOneByUserIdAndName(@Body('userId') userId: number, @Body('name') name: string) {
        return this.ok(await this.pushTemplateService.findOneByUserIdAndName(userId, name));
    }

    @Post('/updateByUserIdAndId', { summary: '根据用户ID和模板ID更新推送模板' })
    async updateByUserIdAndId(@Body('userId') userId: number, @Body('id') id: number, @Body('updateData') updateData: Partial<PushTemplateEntity>) {
        await this.pushTemplateService.updateByUserIdAndId(userId, id, updateData);
        return this.ok();
    }

    @Post('/deleteByUserIdAndId', { summary: '根据用户ID和模板ID删除推送模板' })
    async deleteByUserIdAndId(@Body('userId') userId: number, @Body('id') id: number) {
        await this.pushTemplateService.deleteByUserIdAndId(userId, id);
        return this.ok();
    }

    async page() {
      const { body } = this.baseCtx.request;
      const op = {} as QueryOp
      // let op = this.curdOption.pageQueryOp as QueryOp
      op.where = async (ctx) => {
        return [
          [
            { userId: ctx.admin.userId },
          ],
        ];
      }
      return this.ok(await this.service.page(body, op, this.connectionName));
    }
}
